package com.jboost.modules.app.service.strategry.liangxin;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Objects;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.*;
import com.jboost.modules.system.entity.HsMaterialNeed;
import com.jboost.modules.system.entity.LxPriceData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service(value = "LxLowPriceQuery")
@Slf4j
public class LxLowPriceQuery extends AbstractLxMateria<JsonObject, Boolean> {
    public static final String LX_OTHER_PRICE = "LX_OTHER_PRICE";
    String basePriceFormId = "64c8ae788d3a430008fe52c0";
    String lowPriceFormId = "64ca19a33c957c0009163b93";
    String otherPriceFormId = "64ca241e8e69800008987f94";

    @Async
    @Scheduled(cron = "0 */1 * * * ?")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        lxPriceDataMapper.delete(Wrappers.lambdaQuery(LxPriceData.class).ne(LxPriceData::getId,0));
        log.info("开始批量查询Hs材料数据");
        List<LxPriceData> list = Lists.newArrayList();
        HashMap<String, Object> param = getParam();
        param.put("entry_id",basePriceFormId);
        param.put("limit",1000);
        String base = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
        String baseData = JSONUtil.toJson(JSONObject.parseObject(base).get("data"));
        List<BasePriceJdy> basePriceJdies = JSONArray.parseArray(baseData, BasePriceJdy.class);
        basePriceJdies.forEach(e->{
            LxPriceData lxPriceData = new LxPriceData();
            lxPriceData.setBlpz(e.getPz());
            lxPriceData.setJg("");
            lxPriceData.setHd(0);
            lxPriceData.setDm("否");
            lxPriceData.setContent(JSONUtil.toJson(e));

            list.add(lxPriceData);
        });

        param.put("entry_id",lowPriceFormId);
        String low = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
        String lowData = JSONUtil.toJson(JSONObject.parseObject(low).get("data"));
        List<LowPriceJdy> lowPriceJdies = JSONArray.parseArray(lowData, LowPriceJdy.class);
        lowPriceJdies.forEach(e->{
            LxPriceData lxPriceData = new LxPriceData();
            lxPriceData.setHd(e.getHd());
            lxPriceData.setDm("是");
            lxPriceData.setDmlx(e.getDc());
            lxPriceData.setContent(JSONUtil.toJson(e));
            list.add(lxPriceData);
        });

        param.put("entry_id",otherPriceFormId);
        String other = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
        String data = JSONUtil.toJson(JSONObject.parseObject(other).get("data"));
        List<LxOtherPrice> lxOtherPrices = JSONArray.parseArray(data, LxOtherPrice.class);


        list.forEach(e->{
            lxPriceDataMapper.insert(e);
        });

        redisTemplate.opsForValue().set(LX_OTHER_PRICE,JSONUtil.toJson(lxOtherPrices.stream().filter(e-> Objects.equal(e.getStatus(),"激活")).findFirst().get()));
        redisTemplate.expire(LX_OTHER_PRICE,365, TimeUnit.DAYS);
        log.info("开始批量查询Hs材料数据结束");
        return BaseResult.success();
    }
}
